package com._520it.wms.web.controller;

import com._520it.wms.query.OrderChartQueryObject;
import com._520it.wms.query.SaleChartQueryObject;
import com._520it.wms.service.IBrandService;
import com._520it.wms.service.IChartService;
import com._520it.wms.service.IClientService;
import com._520it.wms.service.ISupplierService;
import com._520it.wms.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HASEE on 2017/11/14.
 */
@Controller
@RequestMapping("/chart")
public class ChartContorller {
    @Autowired
    private IChartService chartService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private IClientService clientService;

    @RequestMapping("/orderChart")
    @RequiredPermission("订货报表")
    public String orderChart(Model model, @ModelAttribute("qo") OrderChartQueryObject qo) {
        model.addAttribute("brands", brandService.listAll());
        model.addAttribute("suppliers", supplierService.listAll());
        //静态代码块加载
        model.addAttribute("groupTypes", OrderChartQueryObject.groupTypes);
        model.addAttribute("datas", chartService.orderChart(qo));

        return "chart/orderChart";
    }

    @RequestMapping("/saleChart")
    @RequiredPermission("销售报表")
    public String saleChart(Model model, @ModelAttribute("qo") SaleChartQueryObject qo) {
        model.addAttribute("brands", brandService.listAll());
        model.addAttribute("clients", clientService.listAll());
        //静态代码块加载
        model.addAttribute("groupTypes", SaleChartQueryObject.groupTypes);
        model.addAttribute("datas", chartService.saleChart(qo));

        return "chart/saleChart";
    }

    @RequestMapping("/saleByBar")
    public String saleByBar(Model model, @ModelAttribute("qo") SaleChartQueryObject qo) {
        List<Map<String, Object>> dataList = chartService.saleChart(qo);
        List<Object> groupTypeList = new ArrayList<>();
        ArrayList<Object> valueList = new ArrayList<>();
        for (Map<String, Object> map : dataList) {
            groupTypeList.add(map.get("groupType"));
            valueList.add(map.get("totalAmount"));
        }
        //取到中文名称
        Object groutType = SaleChartQueryObject.groupTypes.get(qo.getGroupType());
        model.addAttribute("groupType", groutType);
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("valueList", JSON.toJSONString(valueList));

        return "chart/saleByBar";
    }

    @RequestMapping("/saleByPie")
    public String saleByPie(Model model, @ModelAttribute("qo") SaleChartQueryObject qo) {
        List<Map<String, Object>> dataList = chartService.saleChart(qo);
        ArrayList<Object> valueList = new ArrayList<>();
        List<Object> groupTypeList = new ArrayList<>();
        //最大值最小值
        BigDecimal maxValue = BigDecimal.ZERO;
        for (Map<String, Object> map : dataList) {
            groupTypeList.add(map.get("groupType"));
            HashMap<Object, Object> valueMap = new HashMap<>();
            valueMap.put("name", map.get("groupType"));
            valueMap.put("value", map.get("totalAmount"));
            if (maxValue.compareTo((BigDecimal) map.get("totalAmount"))<0 ) {
                maxValue=(BigDecimal) map.get("totalAmount");
            }
            valueList.add(valueMap);
        }
        //取到中文名称
        Object groutType = SaleChartQueryObject.groupTypes.get(qo.getGroupType());
        model.addAttribute("maxValue", maxValue);
        model.addAttribute("groupType", groutType);
        model.addAttribute("groupTypeList", JSON.toJSONString(groupTypeList));
        model.addAttribute("valueList", JSON.toJSONString(valueList));

        return "chart/saleByPie";
    }


}
